package com.csmz.mybookreader.View;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.design.widget.NavigationView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.csmz.mybookreader.Adapter.BookAdapter;
import com.csmz.mybookreader.R;
import com.csmz.mybookreader.UI.LoginActivity;
import com.csmz.mybookreader.UI.MyMessageActivity;
import com.csmz.mybookreader.UI.SettingActivity;
import com.csmz.mybookreader.UserBean.BookInfo;
import com.csmz.mybookreader.Utils.AnalysisUtils;

import java.util.ArrayList;
import java.util.List;

public class DrawerView {
    //侧滑菜单栏
    private NavigationView navigationView;
    private Activity mContext;
    private LayoutInflater mInflater;
    private View mCurrentView;
    private List<BookInfo> bookInfoList = new ArrayList<>();

    public DrawerView(Activity context) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext); //为之后将Layout转化为view时用
    }


    private void createView() {
        initView();
        initBookInfo();

    }

    /**
     * 添加数据
     */
    private void initBookInfo() {
        for (int i = 0; i < 2; i++) {
            BookInfo f1 = new BookInfo("001", R.drawable.head_img_write_bg, "11111");
            bookInfoList.add(f1);
            BookInfo f2 = new BookInfo("001", R.drawable.head_img_write_bg, "11111");
            bookInfoList.add(f2);
            BookInfo f3 = new BookInfo("001", R.drawable.head_img_write_bg, "11111");
            bookInfoList.add(f3);
            BookInfo f4 = new BookInfo("001", R.drawable.head_img_write_bg, "11111");
            bookInfoList.add(f4);
            BookInfo f5 = new BookInfo("001", R.drawable.head_img_write_bg, "11111");
            bookInfoList.add(f5);
            BookInfo f6 = new BookInfo("001", R.drawable.head_img_write_bg, "11111");
            bookInfoList.add(f6);
            BookInfo f7 = new BookInfo("001", R.drawable.head_img_write_bg, "11111");
            bookInfoList.add(f7);
            BookInfo f8 = new BookInfo("001", R.drawable.head_img_write_bg, "11111");
            bookInfoList.add(f8);
            BookInfo f9 = new BookInfo("001", R.drawable.head_img_write_bg, "11111");
            bookInfoList.add(f9);
        }
    }

    /**
     * 获取界面控件
     */
    private void initView() {
        //设置布局文件
        mCurrentView = mInflater.inflate(R.layout.activity_drawer, null);
        navigationView = mCurrentView.findViewById(R.id.nav);

        BookAdapter bookAdapter = new BookAdapter(mContext, R.layout.drawer_item, bookInfoList);
        ListView listView = mCurrentView.findViewById(R.id.main_drawer_lv);
        listView.setAdapter(bookAdapter);

        //mCurrentView = navigationView.getHeaderView(0);
        //开启手势滑动打开侧滑菜单栏，如果要关闭手势滑动，将后面的UNLOCKED替换成LOCKED_CLOSED 即可
        //drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

        /**
         * 侧滑菜单item点击事件
         */
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_myMsg:
                        if (TextUtils.isEmpty(AnalysisUtils.readLoginUserName(mContext))){
                            Toast.makeText(mContext, "请登录", Toast.LENGTH_SHORT).show();
                        }else {
                            Intent intent=new Intent(mContext,MyMessageActivity.class);
                            mContext.startActivity(intent);
                        }
                        //Toast.makeText(mContext, item.getTitle().toString(), Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.menu_Setting:
                        if (TextUtils.isEmpty(AnalysisUtils.readLoginUserName(mContext))){
                            Toast.makeText(mContext, "请登录", Toast.LENGTH_SHORT).show();
                        }else {

                            mContext.startActivity(new Intent(mContext, SettingActivity.class));
                        }
                        //Toast.makeText(mContext, item.getTitle().toString(), Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.menu_Feedback:
                        Toast.makeText(mContext, item.getTitle().toString(), Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.menu_AboutUs:
                        Toast.makeText(mContext, item.getTitle().toString(), Toast.LENGTH_SHORT).show();
                        break;

                }
                //drawerLayout.closeDrawer(navigationView);
                return true;
            }
        });

    }

    /**
     * 获取当前在导航栏上方显示对应的view
     */
    public View getView() {
        if (mCurrentView == null) {
            createView();
        }
        return mCurrentView;
    }

    /**
     * 显示在当前导航栏上显示对应的view
     */
    public void showView() {
        if (mCurrentView == null) {
            createView();
        }
        mCurrentView.setVisibility(View.VISIBLE);
    }

    //从SharedPreferences中读取登录用户名
    private boolean readLoginStatus() {
        SharedPreferences sp = mContext.getSharedPreferences("LoginInfo", Context.MODE_PRIVATE);
        boolean isLogin=sp.getBoolean("isLogin",false);
        return isLogin;
    }

}